package com.bdult.vh.controller;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import oracle.sql.CLOB;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bdult.vh.common.CommonUtil;
import com.bdult.vh.service.BoardService;
import com.bdult.vh.service.ItemService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class BoardController {
	
	private static final Logger logger = LoggerFactory.getLogger(BoardController.class);
	
	@Autowired
	private BoardService boardService;
	
	@Autowired
	private ItemService itemService;
	
	@RequestMapping(value="notice")
	public ModelAndView notice(Model model, @RequestParam Map<String, Object> paramMap) throws SQLException, IOException {
		
		ModelAndView mv = new ModelAndView();
		
		//paging
		int current_page = 0;
		int max_elem = 10;
		System.out.println("****** paramMap = " + paramMap);
		if(paramMap.get("current_page")!=null) {
			current_page = Integer.parseInt( (String) paramMap.get("current_page") );
		}
		if(paramMap.get("max_elem")!=null) {
			max_elem = Integer.parseInt( (String) paramMap.get("max_elem") );
		}
		mv.addObject("current_page", current_page);
		mv.addObject("max_elem", max_elem);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("START_NUM", current_page * 10);
		map.put("END_NUM", max_elem);
		

		ArrayList<HashMap<String, Object>> rNoticeList =  new ArrayList<HashMap<String,Object>>();
		System.out.println("****** map = " + map);
		ArrayList<HashMap<String, Object>> noticeList =  boardService.noticeList(map);
		String tot_cnt = "0";		
		for(HashMap<String, Object> notice : noticeList) {
			CLOB clob = (CLOB) notice.get("BOARDDESC");
			  
			Reader reader = clob.getCharacterStream();
			  
			StringBuffer out = new StringBuffer();
			char[] buff = new char[1024];
			int nchars = 0;
			  
			while((nchars = reader.read(buff))>0){
			   out.append(buff,0,nchars);
			}
			
			notice.put("BOARDDESC_ENC", out.toString());
			
			rNoticeList.add(notice);
			//총 갯수 구하기
			tot_cnt = String.valueOf( notice.get("TOT_CNT") );
		}
		
		mv.addObject("tot_cnt", tot_cnt);
		mv.addObject("type", "notice");
		mv.addObject("noticeList", rNoticeList);
		mv.setViewName("notice");
		return mv;
	}
	
	@RequestMapping(value="notice_event")
	public ModelAndView notice_event(Model model, @RequestParam Map<String, Object> paramMap) throws SQLException, IOException {
		
		ModelAndView mv = new ModelAndView();
		
		//paging
		int current_page = 0;
		int max_elem = 10;
		System.out.println("****** paramMap = " + paramMap);
		if(paramMap.get("current_page")!=null) {
			current_page = Integer.parseInt( (String) paramMap.get("current_page") );
		}
		if(paramMap.get("max_elem")!=null) {
			max_elem = Integer.parseInt( (String) paramMap.get("max_elem") );
		}
		mv.addObject("current_page", current_page);
		mv.addObject("max_elem", max_elem);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("START_NUM", current_page * 10);
		map.put("END_NUM", max_elem);
		

		ArrayList<HashMap<String, Object>> rNoticeList =  new ArrayList<HashMap<String,Object>>();
		System.out.println("****** map = " + map);
		ArrayList<HashMap<String, Object>> noticeList =  boardService.noticeList_event(map);
		String tot_cnt = "0";		
		for(HashMap<String, Object> notice : noticeList) {
			CLOB clob = (CLOB) notice.get("BOARDDESC");
			  
			Reader reader = clob.getCharacterStream();
			  
			StringBuffer out = new StringBuffer();
			char[] buff = new char[1024];
			int nchars = 0;
			  
			while((nchars = reader.read(buff))>0){
			   out.append(buff,0,nchars);
			}
			
			notice.put("BOARDDESC_ENC", out.toString());
			
			rNoticeList.add(notice);
			//총 갯수 구하기
			tot_cnt = String.valueOf( notice.get("TOT_CNT") );
		}
		
		mv.addObject("tot_cnt", tot_cnt);
		mv.addObject("type", "notice_event");
		mv.addObject("noticeList", rNoticeList);
		mv.setViewName("notice");
		return mv;
	}
	
	@RequestMapping(value="qna_write")
	public ModelAndView qna_write(Model model, @RequestParam Map<String, Object> paramMap) throws SQLException, IOException {
		ModelAndView mv = new ModelAndView();
		
		String prdcd = paramMap.get("prdcd").toString();
		Map<String,String> item = itemService.getItemData(prdcd);		
		String prdimg1 = item.get("PRDIMG1");
		prdimg1 = CommonUtil.encodeURL(prdimg1, "utf-8");
		item.put("PRDIMG1_ENC", prdimg1);
		mv.addObject("item", item);
		
		mv.setViewName("qna_write");
		return mv;
	}
}
